###############################################################
#An Unexpected Short: How Livestream Selling Shapes Firm Value
###############################################################

###set working directory
  setwd("./Influencer")

###library
  library(data.table)
  library(lfe)
  library(stargazer)

###main effect
for(i in 0:4){
  for (j in -2:0){
    ##set parameter
      #time window forward
      lag=i
      #time window backward
      advance=j
    ##aggregate data to stock-day level
      temp=data.frame(MarketStock=rep(LiveProductWithStock$MarketStock,times=rep(lag-advance+1,nrow(LiveProductWithStock))),
                      CompanyNameChinese=rep(LiveProductWithStock$CompanyNameChinese,times=rep(lag-advance+1,nrow(LiveProductWithStock))),
                      Date=rep(LiveProductWithStock$Date,times=rep(lag-advance+1,nrow(LiveProductWithStock))),
                      DateInd=rep(LiveProductWithStock$DateInd,times=rep(lag-advance+1,nrow(LiveProductWithStock))),
                      Lag=rep((advance):lag,times=nrow(LiveProductWithStock)))
      temp$DateIndLag=temp$DateInd+temp$Lag
    ##merge abnormal return and weibo/news to the livestreaming
      #merge abnormal return
      temp=merge(temp,StockPrice[,c('MarketStock','DateInd','AbnormalReturn')],
                 by.x=c('MarketStock','DateIndLag'),by.y=c('MarketStock','DateInd'),all.x=T)
      temp=as.data.table(temp)
      temp=temp[,.(AbnormalReturn=sum(AbnormalReturn,na.rm=T)),by=.(MarketStock,Date)]
      LiveProductWithStock=merge(LiveProductWithStock,temp,
                                          by=c('MarketStock','Date'),
                                          all.x=T)
    #store results
    result=t.test(LiveProductWithStock$AbnormalReturn)
  }
}

###regression analysis
result1=felm(AbnormalReturn~log(Revenue+1)+ValuePremium+
               log(Follower+1)+FitPlatform+
               log(NAnnoucement+1)+TotalLeverage+log(SellingCost+1)+log(DirectorsNumber+1)+ProductService+
               log(NWeibo+1)+log(NNews+1)+
               log(Frequency+1)+log(FrequencyInfluencer)+
               log(Price+1)+log(SaleVolumeProduct+1)+ReturnRate+Commission+log(OfficialStore+1)+
               log(LiveStreamProductN+1)+CelebrityCoder+InverseMillsRatio+InverseMillsRatioInfluencer|BrandCategory+Month,data=LiveProductWithStock)
stargazer(result1)

